package com.gqj.elec.service.impl;

import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.uuid.HiultraUtil;
import com.gqj.elec.domain.ElecMaterial;
import com.gqj.elec.domain.ElecMaterialOutdetail;
import com.gqj.elec.domain.ElecMaterialOutinfo;
import com.gqj.elec.mapper.ElecMaterialMapper;
import com.gqj.elec.mapper.ElecMaterialOutdetailMapper;
import com.gqj.elec.mapper.ElecMaterialOutinfoMapper;
import com.gqj.elec.service.IElecMaterialOutinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 物资出库Service业务层处理
 *
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class ElecMaterialOutinfoServiceImpl implements IElecMaterialOutinfoService {
    @Autowired
    private ElecMaterialOutinfoMapper elecMaterialOutinfoMapper;
    @Autowired
    private ElecMaterialOutdetailMapper elecMaterialOutdetailMapper;
    @Autowired
    private ElecMaterialMapper elecMaterialMapper;

    /**
     * 查询物资出库
     *
     * @param outId 物资出库主键
     * @return 物资出库
     */
    @Override
    public ElecMaterialOutinfo selectElecMaterialOutinfoByOutId(Long outId) {
        ElecMaterialOutinfo elecMaterialOutinfo = elecMaterialOutinfoMapper.selectElecMaterialOutinfoByOutId(outId);

        ElecMaterialOutdetail elecMaterialOutdetail = new ElecMaterialOutdetail();
        elecMaterialOutdetail.setOutId(outId);

        List<ElecMaterialOutdetail> elecMaterialList = elecMaterialOutdetailMapper.selectElecMaterialOutdetailList(elecMaterialOutdetail);
        elecMaterialOutinfo.setElecMaterialList(elecMaterialList);
        return elecMaterialOutinfo;
    }

    /**
     * 查询物资出库列表
     *
     * @param elecMaterialOutinfo 物资出库
     * @return 物资出库
     */
    @Override
    public List<ElecMaterialOutinfo> selectElecMaterialOutinfoList(ElecMaterialOutinfo elecMaterialOutinfo) {
        return elecMaterialOutinfoMapper.selectElecMaterialOutinfoList(elecMaterialOutinfo);
    }

    /**
     * 新增物资出库
     *
     * @param elecMaterialOutinfo 物资出库
     * @return 结果
     */
    @Override
    @Transactional
    public int insertElecMaterialOutinfo(ElecMaterialOutinfo elecMaterialOutinfo) {
        Date nowDate = DateUtils.getNowDate();
        elecMaterialOutinfo.setCreateTime(nowDate);
        elecMaterialOutinfo.setBatchNumber("CK-" + HiultraUtil.getOrderNo());
        elecMaterialOutinfo.setOutStatus("1");
        elecMaterialOutinfo.setOutType("1");
        int i = elecMaterialOutinfoMapper.insertElecMaterialOutinfo(elecMaterialOutinfo);

        List<ElecMaterialOutdetail> elecMaterialList = elecMaterialOutinfo.getElecMaterialList();
        for (ElecMaterialOutdetail material : elecMaterialList) {
            ElecMaterialOutdetail elecMaterialOutdetail = new ElecMaterialOutdetail();
            elecMaterialOutdetail.setOutId(elecMaterialOutinfo.getOutId());
            elecMaterialOutdetail.setMaterialId(material.getMaterialId());
            elecMaterialOutdetail.setCreateBy(elecMaterialOutinfo.getCreateBy());
            elecMaterialOutdetail.setCreateTime(nowDate);
            elecMaterialOutdetail.setOutNumber(material.getOutNumber());
            if (elecMaterialOutinfo.getMaterialCategory().equals("3")) {
                elecMaterialOutdetail.setIsOut("1");
            } else {
                elecMaterialOutdetail.setIsOut("0");
                elecMaterialOutdetail.setIsReturn("0");
            }
            elecMaterialOutdetailMapper.insertElecMaterialOutdetail(elecMaterialOutdetail);

            ElecMaterial temp = new ElecMaterial();
            temp.setMaterialId(material.getMaterialId());

            if (elecMaterialOutinfo.getMaterialCategory().equals("3")) {
                if(null == material.getOutNumber() || material.getOutNumber() <= 0){
                    throw new ServiceException(material.getMaterialName() + "的出库数量小于等于0或为空，无法进行出库操作！");
                }

                Integer numberResult = 0;
                ElecMaterial elecMaterial = elecMaterialMapper.selectElecMaterialByMaterialId(material.getMaterialId());
                Integer number = elecMaterial.getNumber();
                if (null != number) {
                    if (material.getOutNumber() > number) {
                        throw new ServiceException(elecMaterial.getMaterialName() + "的出库数量比当前数量大，无法进行出库操作！");
                    }
                } else {
                    throw new ServiceException(elecMaterial.getMaterialName() + "的当前数量值为空，无法进行出库操作！");
                }
                numberResult = number - material.getOutNumber();
                temp.setNumber(numberResult);
            } else {
                temp.setMaterialStatus("21");
            }
            elecMaterialMapper.updateElecMaterial(temp);
        }
        return i;
    }

    /**
     * 修改物资出库
     *
     * @param elecMaterialOutinfo 物资出库
     * @return 结果
     */
    @Override
    @Transactional
    public int updateElecMaterialOutinfo(ElecMaterialOutinfo elecMaterialOutinfo) {
        elecMaterialOutinfo.setUpdateTime(DateUtils.getNowDate());
        int i = elecMaterialOutinfoMapper.updateElecMaterialOutinfo(elecMaterialOutinfo);

        ElecMaterialOutinfo entity = elecMaterialOutinfoMapper.selectElecMaterialOutinfoByOutId(elecMaterialOutinfo.getOutId());

        ElecMaterialOutdetail elecMaterialOutdetail = new ElecMaterialOutdetail();
        elecMaterialOutdetail.setOutId(entity.getOutId());
        List<ElecMaterialOutdetail> elecMaterialList = elecMaterialOutdetailMapper.selectElecMaterialOutdetailList(elecMaterialOutdetail);
        for (ElecMaterialOutdetail emo : elecMaterialList) {
            if (null == emo.getSparePartType() || "".equals(emo.getSparePartType()) || "1".equals(emo.getSparePartType())) {
                ElecMaterial temp = new ElecMaterial();
                temp.setMaterialId(emo.getMaterialId());
                temp.setMaterialStatus("1");
                elecMaterialMapper.updateElecMaterial(temp);
            }
        }
        elecMaterialOutdetailMapper.deleteElecMaterialOutdetailByoutId(elecMaterialOutinfo.getOutId());

        List<ElecMaterialOutdetail> elecMaterialListT = elecMaterialOutinfo.getElecMaterialList();
        for (ElecMaterialOutdetail material : elecMaterialListT) {
            ElecMaterialOutdetail elecMaterialOutdetailT = new ElecMaterialOutdetail();
            elecMaterialOutdetailT.setOutId(elecMaterialOutinfo.getOutId());
            elecMaterialOutdetailT.setMaterialId(material.getMaterialId());
            elecMaterialOutdetailT.setUpdateBy(elecMaterialOutinfo.getUpdateBy());
            elecMaterialOutdetailT.setCreateTime(DateUtils.getNowDate());
            elecMaterialOutdetailT.setIsOut("0");
            elecMaterialOutdetailT.setIsReturn("0");
            elecMaterialOutdetailMapper.insertElecMaterialOutdetail(elecMaterialOutdetailT);

            if (null == material.getSparePartType() || "".equals(material.getSparePartType()) || "1".equals(material.getSparePartType())) {
                ElecMaterial temp = new ElecMaterial();
                temp.setMaterialId(material.getMaterialId());
                temp.setMaterialStatus("21");
                elecMaterialMapper.updateElecMaterial(temp);
            }
        }

        return i;
    }

    /**
     * 批量删除物资出库
     *
     * @param outIds 需要删除的物资出库主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteElecMaterialOutinfoByOutIds(Long[] outIds) {
        for (Long outId : outIds) {
            ElecMaterialOutinfo entity = elecMaterialOutinfoMapper.selectElecMaterialOutinfoByOutId(outId);

            ElecMaterialOutdetail elecMaterialOutdetail = new ElecMaterialOutdetail();
            elecMaterialOutdetail.setOutId(entity.getOutId());
            List<ElecMaterialOutdetail> elecMaterialList = elecMaterialOutdetailMapper.selectElecMaterialOutdetailList(elecMaterialOutdetail);
            for (ElecMaterialOutdetail emo : elecMaterialList) {
                if (null == emo.getSparePartType() || "".equals(emo.getSparePartType()) || "1".equals(emo.getSparePartType())) {
                    ElecMaterial temp = new ElecMaterial();
                    temp.setMaterialId(emo.getMaterialId());
                    temp.setMaterialStatus("1");
                    elecMaterialMapper.updateElecMaterial(temp);
                }
            }
        }
        return elecMaterialOutinfoMapper.deleteElecMaterialOutinfoByOutIds(outIds);
    }

    /**
     * 删除物资出库信息
     *
     * @param outId 物资出库主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialOutinfoByOutId(Long outId) {
        return elecMaterialOutinfoMapper.deleteElecMaterialOutinfoByOutId(outId);
    }
}
